package org.cyz.tools.core.handler;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import org.w3c.dom.Node;

/**
 * @program: RedisGUI
 * @description: 拖拽监听器
 * @author: Chenyz
 * @create: 2020-08-08 21:47
 **/
public class DragListener implements EventHandler<MouseEvent> {

    private double xOffset=0;
    private double yOffset=0;
    private final Stage stage;

    public DragListener(Stage stage) {
        this.stage = stage;
    }


    @Override
    public void handle(MouseEvent mouseEvent) {
        mouseEvent.consume();
        if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
            xOffset = mouseEvent.getSceneX();
            yOffset = mouseEvent.getSceneY();
        } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            stage.setX(mouseEvent.getScreenX() - xOffset);
            if(mouseEvent.getScreenY() - yOffset < 0) {
                stage.setY(0);
            }else {
                stage.setY(mouseEvent.getScreenY() - yOffset);
            }
        }
    }

    public void enableDrag(Node node) {
//        node.setOnMousePressed(this);
//        node.setOnMouseDragged(this);
    }

}
